package com.yihusitian.zsxq.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yihusitian.zsxq.pojo.data.UserDO;
import com.yihusitian.zsxq.pojo.dto.Comment;
import com.yihusitian.zsxq.service.ImageService;
import com.yihusitian.zsxq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihusitian.zsxq.service.CommentService;
import com.yihusitian.zsxq.pojo.data.CommentDO;
import com.yihusitian.zsxq.mapper.CommentMapper;

import java.util.List;

/**
 * @Description TODO
 * @Author LeeHo
 * @Date 2021-03-25 17:00:14
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> implements CommentService {

    @Autowired
    private UserService userService;

    @Autowired
    private ImageService imageService;

    @Override
    public void createComments(List<Comment> show_comments, Long topicId) {
        if (CollectionUtil.isNotEmpty(show_comments)) {
            show_comments.forEach(comment -> {
                Long commentId = comment.getComment_id();
                UserDO owner = userService.createOrUpdate(comment.getOwner());
                CommentDO commentDO = CommentDO.builder().id(commentId)
                        .ownerId(owner.getId())
                        .topicId(topicId)
                        .text(StrUtil.emptyToDefault(comment.getText(), StrUtil.EMPTY))
                        .rewardsCount(comment.getRewards_count())
                        .likesCount(comment.getLikes_count())
                        .repliesCount(comment.getReplies_count())
                        .createTime(comment.getCreateTime())
                        .build();
                commentDO.insert();
                imageService.createImages(comment.getImages(), commentId);

            });
        }
    }
}